﻿using System;
using System.Data;

using ESRI.ArcGIS.esriSystem;

/// <summary>
/// AoLicenseInit 的摘要说明
/// </summary>
public class AoLicenseManager
{
    protected IAoInitialize m_AoInitialize = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
    public AoLicenseManager()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public bool Initialize()
    {
        bool bInitialized = true;
        if (this.m_AoInitialize == null)
        {
            bInitialized = false;
        }
        //初始化应用程序
        esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;

        licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
        if (licenseStatus != esriLicenseStatus.esriLicenseAlreadyInitialized)
        {
            this.m_AoInitialize.Shutdown();
            licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
            if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
            {
                licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcView);
                if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                {
                    licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
                    if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                    {
                        licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
                        if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                        {
                            bInitialized = false;
                        }
                    }
                }
            }
        }
        return bInitialized;
        //m_AoInitialize.Shutdown
    }

    private esriLicenseStatus CheckOutLicenses(esriLicenseProductCode productCode)
    {
        esriLicenseStatus licenseStatus;

        //是否产品是可能的
        licenseStatus = m_AoInitialize.IsProductCodeAvailable(productCode);
        if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)
        {

            //用相应的许可文件进行初始化
            licenseStatus = m_AoInitialize.Initialize(productCode);
        }
        return licenseStatus;
    }

    public bool Shutdown()
    {
        try
        {
            this.m_AoInitialize.Shutdown();
            return true;
        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message.ToString());
            return false;
        }
    }


}
